package fairygui.editor.publish
{
   import fairygui.editor.animation.AniDef;
   import fairygui.editor.animation.AniFrame;
   import fairygui.editor.animation.AniTexture;
   import fairygui.editor.gui.EPackageItem;
   import fairygui.editor.gui.EUIPackage;
   import fairygui.editor.utils.UtilsFile;
   import fairygui.editor.utils.UtilsStr;
   
   public class CollectItems extends PublishStep
   {
       
      
      private var _excludeList:Object;
      
      private var _binMap:Object;
      
      public var excludedCount:int;
      
      public function CollectItems()
      {
         super();
      }
      
      override public function run() : void
      {
         var _loc3_:EPackageItem = null;
         var _loc4_:String = null;
         this.excludedCount = 0;
         this._binMap = {};
         var _loc1_:Array = publishData.pkg.publishSettings.excludedList;
         if(_loc1_.length > 0)
         {
            this._excludeList = {};
            for each(_loc4_ in _loc1_)
            {
               this._excludeList[_loc4_] = false;
            }
         }
         var _loc2_:Vector.<EPackageItem> = publishData.pkg.resources;
         for each(_loc3_ in _loc2_)
         {
            _loc3_.addedToPublish = false;
         }
         for each(_loc3_ in _loc2_)
         {
            if(_loc3_.exported)
            {
               this.addItem(_loc3_);
            }
         }
         publishData.items.sortOn(["exported","type","id"]);
         for each(_loc3_ in publishData.items)
         {
            _loc3_.touch();
            if(_loc3_.errorStatus != 0)
            {
               stepCallback.addMsg(_loc3_.path + _loc3_.name + " not exists!");
            }
         }
         stepCallback.callOnSuccess();
      }
      
      private function addItem(param1:EPackageItem) : void
      {
         var _loc2_:AniDef = null;
         var _loc3_:int = 0;
         var _loc4_:AniTexture = null;
         var _loc5_:int = 0;
         var _loc6_:AniFrame = null;
         if(param1.addedToPublish)
         {
            return;
         }
         if(this._excludeList && this._excludeList[param1.id] != undefined)
         {
            if(this._excludeList[param1.id] == false)
            {
               this._excludeList[param1.id] = true;
               this.excludedCount++;
            }
            return;
         }
         param1.addedToPublish = true;
         publishData.items.push(param1);
         if(param1.type == EPackageItem.IMAGE)
         {
            if(publishData.usingAtlas)
            {
               this.addToAtlas(param1);
            }
         }
         else if(param1.type == EPackageItem.MOVIECLIP)
         {
            if(publishData.usingAtlas)
            {
               this.addToAtlas(param1);
            }
            _loc2_ = publishData.pkg.getMovieClip(param1);
            _loc3_ = _loc2_.frameCount;
            for each(_loc4_ in _loc2_.textureList)
            {
               _loc4_.exportFrame = -1;
            }
            _loc5_ = 0;
            while(_loc5_ < _loc3_)
            {
               _loc6_ = _loc2_.frameList[_loc5_];
               if(_loc6_.textureIndex != -1)
               {
                  _loc4_ = _loc2_.textureList[_loc6_.textureIndex];
                  if(_loc4_.raw != null && _loc4_.exportFrame == -1)
                  {
                     _loc4_.exportFrame = _loc5_;
                  }
               }
               _loc5_++;
            }
         }
         if(param1.type == EPackageItem.COMPONENT)
         {
            this.getComponentDependencies(param1);
         }
         else if(param1.type == EPackageItem.FONT)
         {
            this.getFontDependencies(param1);
         }
      }
      
      private function addToAtlas(param1:EPackageItem) : void
      {
         var _loc2_:String = null;
         var _loc3_:int = 0;
         if(param1.imageSetting.atlas == "default")
         {
            _loc2_ = "atlas0";
            _loc3_ = 0;
         }
         else if(param1.imageSetting.atlas == "alone" || param1.imageSetting.atlas == "alone_npot")
         {
            _loc2_ = "atlas_" + param1.id;
            _loc3_ = -1;
         }
         else
         {
            _loc2_ = "atlas" + param1.imageSetting.atlas;
            _loc3_ = parseInt(param1.imageSetting.atlas);
         }
         var _loc4_:AtlasItem = this._binMap[_loc2_];
         if(!_loc4_)
         {
            _loc4_ = new AtlasItem();
            _loc4_.id = _loc2_;
            _loc4_.index = _loc3_;
            publishData.atlases.push(_loc4_);
            this._binMap[_loc2_] = _loc4_;
         }
         _loc4_.items.push(param1);
         if(param1.imageInfo.format != "jpg")
         {
            _loc4_.alphaChannel = true;
         }
      }
      
      private function addURL(param1:String) : void
      {
         var _loc2_:int = 0;
         var _loc3_:String = null;
         var _loc4_:EUIPackage = null;
         var _loc5_:String = null;
         var _loc6_:EPackageItem = null;
         if(UtilsStr.startsWith(param1,"ui://"))
         {
            _loc2_ = param1.indexOf(",");
            if(_loc2_ != -1)
            {
               param1 = param1.substr(0,_loc2_);
            }
            _loc3_ = param1.substr(5,8);
            _loc4_ = publishData._project.getPackage(_loc3_);
            if(!_loc4_ || _loc4_ != publishData.pkg)
            {
               return;
            }
            _loc5_ = param1.substr(13);
            _loc6_ = _loc4_.getItem(_loc5_);
            if(_loc6_)
            {
               this.addItem(_loc6_);
            }
         }
      }
      
      private function getComponentDependencies(param1:EPackageItem) : void
      {
         var col:Object = null;
         var col2:XMLList = null;
         var cxml:XML = null;
         var dxml:XML = null;
         var ename:String = null;
         var cname:String = null;
         var str:String = null;
         var arr:Array = null;
         var src:String = null;
         var k:int = 0;
         var i:int = 0;
         var xml:XML = null;
         var pkgId:String = null;
         var pi2:EPackageItem = null;
         var gid:String = null;
         var pgid:String = null;
         var exml:XML = null;
         var apkg:EUIPackage = null;
         var pi:EPackageItem = param1;
         try
         {
            xml = UtilsFile.loadXML(pi.file);
            if(!xml)
            {
               return;
            }
         }
         catch(err:Error)
         {
            stepCallback.addMsg("XML format error: " + pi.name);
            return;
         }
         delete xml.@resolution;
         delete xml.@copies;
         delete xml.@designImage;
         delete xml.@designImageOffsetX;
         delete xml.@designImageOffsetY;
         delete xml.@designImageAlpha;
         delete xml.@designImageLayer;
         delete xml.@initName;
         delete xml.@bgColor;
         delete xml.@bgColorEnabled;
         var toDelete:Array = [];
         var classInfo:Object = {};
         classInfo.classId = pi.id;
         classInfo.className = pi.name;
         str = xml.@extention;
         if(str)
         {
            classInfo.superClassName = "G" + str;
         }
         else
         {
            classInfo.superClassName = "GComponent";
         }
         if(str != "ScrollBar")
         {
            publishData.outputClasses[pi.id] = classInfo;
         }
         str = xml.@customExtention;
         if(str)
         {
            classInfo.customSuperClassName = str;
            delete xml.@customExtention;
         }
         str = xml.@remark;
         if(str)
         {
            classInfo.remark = str;
            delete xml.@remark;
         }
         str = xml.@hitTest;
         if(str)
         {
            col = xml.displayList.elements();
            for each(cxml in col)
            {
               if(cxml.@id == str)
               {
                  src = cxml.@src;
                  if(src)
                  {
                     xml.@hitTest = src + "," + cxml.@xy;
                     publishData._hitTestImages.push(src);
                     break;
                  }
               }
            }
         }
         var members:Array = [];
         classInfo.members = members;
         col = xml.controller;
         for each(cxml in col)
         {
            cname = cxml.@name;
            delete cxml.@exported;
            delete cxml.@alias;
            members.push({
               "name":cname,
               "type":"Controller"
            });
         }
         col = xml.displayList.elements();
         for each(cxml in col)
         {
            delete cxml.@aspect;
            delete cxml.@locked;
            delete cxml.@hideByEditor;
            ename = cxml.name().localName;
            cname = cxml.@name;
            src = cxml.@src;
            if(src)
            {
               pkgId = cxml.@pkg;
               if(!pkgId || pkgId == publishData.pkg.id)
               {
                  pi2 = publishData.pkg.getItem(src);
                  if(pi2)
                  {
                     this.addItem(pi2);
                  }
               }
            }
            switch(ename)
            {
               case "loader":
                  this.addURL(cxml.@url);
                  members.push({
                     "name":cname,
                     "type":"GLoader"
                  });
                  break;
               case "list":
                  this.addURL(cxml.@defaultItem);
                  members.push({
                     "name":cname,
                     "type":"GList"
                  });
                  col2 = cxml.item;
                  for each(exml in col2)
                  {
                     str = String(exml.@url);
                     if(str)
                     {
                        this.addURL(str);
                     }
                     str = String(exml.@icon);
                     if(str)
                     {
                        this.addURL(exml.@icon);
                     }
                  }
                  str = cxml.@scrollBarRes;
                  if(str)
                  {
                     arr = str.split(",");
                     this.addURL(arr[0]);
                     this.addURL(arr[1]);
                  }
                  str = cxml.@ptrRes;
                  if(str)
                  {
                     arr = str.split(",");
                     this.addURL(arr[0]);
                     this.addURL(arr[1]);
                  }
                  break;
               case "group":
                  str = cxml.@advanced;
                  if(str != "true")
                  {
                     toDelete.push(cxml.childIndex());
                     gid = cxml.@id;
                     pgid = cxml.@group;
                     for each(exml in col)
                     {
                        if(exml.@group == gid)
                        {
                           if(pgid)
                           {
                              exml.@group = pgid;
                           }
                           else
                           {
                              delete exml.@group;
                           }
                        }
                     }
                  }
                  else
                  {
                     delete cxml.@collapsed;
                     members.push({
                        "name":cname,
                        "type":"GGroup"
                     });
                  }
                  break;
               case "text":
                  str = cxml.@input;
                  if(str == "true")
                  {
                     members.push({
                        "name":cname,
                        "type":"GTextInput"
                     });
                  }
                  else
                  {
                     members.push({
                        "name":cname,
                        "type":"GTextField"
                     });
                  }
                  break;
               case "richtext":
                  members.push({
                     "name":cname,
                     "type":"GRichTextField"
                  });
                  break;
               case "movieclip":
                  members.push({
                     "name":cname,
                     "type":"GMovieClip"
                  });
                  break;
               case "jta":
                  cxml.setLocalName("movieclip");
                  members.push({
                     "name":cname,
                     "type":"GMovieClip"
                  });
                  break;
               case "image":
                  str = cxml.@forHitTest;
                  if(str == "true")
                  {
                     if(src)
                     {
                        xml.@hitTest = src + "," + cxml.@xy;
                        publishData._hitTestImages.push(src);
                     }
                     delete cxml.@forHitTest;
                  }
                  str = cxml.@forMask;
                  if(str == "true")
                  {
                     xml.@mask = cxml.@id;
                     delete cxml.@forMask;
                  }
                  members.push({
                     "name":cname,
                     "type":"GImage"
                  });
                  break;
               case "swf":
                  members.push({
                     "name":cname,
                     "type":"GSwfObject"
                  });
                  break;
               case "graph":
                  str = cxml.@forMask;
                  if(str == "true")
                  {
                     xml.@mask = cxml.@id;
                     delete cxml.@forMask;
                  }
                  members.push({
                     "name":cname,
                     "type":"GGraph"
                  });
                  break;
               case "component":
                  if(!pkgId || pkgId == publishData.pkg.id)
                  {
                     members.push({
                        "name":cname,
                        "type":"GComponent",
                        "src":publishData.pkg.getItemName(src),
                        "src_id":src
                     });
                  }
                  else
                  {
                     apkg = publishData._project.getPackage(pkgId);
                     if(apkg != null)
                     {
                        members.push({
                           "name":cname,
                           "type":"GComponent",
                           "src":apkg.getItemName(src),
                           "src_id":src,
                           "pkg":apkg.name,
                           "pkg_id":apkg.id
                        });
                     }
                  }
                  dxml = cxml.Button[0];
                  if(dxml)
                  {
                     this.addURL(dxml.@icon);
                     this.addURL(dxml.@selectedIcon);
                     this.addURL(dxml.@sound);
                  }
                  dxml = cxml.Label[0];
                  if(dxml)
                  {
                     this.addURL(dxml.@icon);
                  }
                  dxml = cxml.ComboBox[0];
                  if(dxml)
                  {
                     col2 = dxml.item;
                     for each(exml in col2)
                     {
                        str = exml.@icon;
                        if(str)
                        {
                           this.addURL(str);
                        }
                     }
                  }
            }
            dxml = cxml.gearIcon[0];
            if(dxml)
            {
               str = dxml.@values;
               arr = str.split("|");
               for each(str in arr)
               {
                  this.addURL(str);
               }
               str = dxml["default"];
               this.addURL(str);
            }
         }
         k = 0;
         i = 0;
         while(i < toDelete.length)
         {
            delete col[toDelete[i] - k];
            k++;
            i++;
         }
         dxml = xml.Button[0];
         if(dxml)
         {
            this.addURL(dxml.@sound);
         }
         dxml = xml.ComboBox[0];
         if(dxml)
         {
            this.addURL(dxml.@dropdown);
         }
         col = xml.transition;
         for each(cxml in col)
         {
            cname = cxml.@name;
            members.push({
               "name":cname,
               "type":"Transition"
            });
            col2 = cxml.item;
            for each(dxml in col2)
            {
               if(dxml.@type == "Sound")
               {
                  this.addURL(dxml.@value);
               }
            }
         }
         str = xml.@scrollBarRes;
         if(str)
         {
            arr = str.split(",");
            this.addURL(arr[0]);
            this.addURL(arr[1]);
         }
         str = xml.@ptrRes;
         if(str)
         {
            arr = str.split(",");
            this.addURL(arr[0]);
            this.addURL(arr[1]);
         }
         publishData.outputDesc[pi.id + ".xml"] = xml;
      }
      
      private function getFontDependencies(param1:EPackageItem) : void
      {
         var _loc5_:int = 0;
         var _loc7_:String = null;
         var _loc8_:Array = null;
         var _loc9_:int = 0;
         var _loc10_:Array = null;
         var _loc11_:EPackageItem = null;
         var _loc2_:String = UtilsFile.loadString(param1.file);
         if(!_loc2_)
         {
            return;
         }
         var _loc3_:Array = _loc2_.split("\n");
         var _loc4_:int = _loc3_.length;
         var _loc6_:Object = {};
         _loc5_ = 0;
         while(_loc5_ < _loc4_)
         {
            _loc7_ = _loc3_[_loc5_];
            if(_loc7_)
            {
               _loc8_ = _loc7_.split(" ");
               _loc9_ = 1;
               while(_loc9_ < _loc8_.length)
               {
                  _loc10_ = _loc8_[_loc9_].split("=");
                  _loc6_[_loc10_[0]] = _loc10_[1];
                  _loc9_++;
               }
               _loc7_ = _loc8_[0];
               if(_loc7_ == "char")
               {
                  this.addURL("ui://" + publishData.pkg.id + _loc6_.img);
               }
               else if(_loc7_ == "info")
               {
                  if(_loc6_.face != undefined)
                  {
                     break;
                  }
               }
            }
            _loc5_++;
         }
         if(param1.fontTexture)
         {
            _loc11_ = publishData.pkg.getItem(param1.fontTexture);
            if(_loc11_)
            {
               publishData._fontTextures[param1.fontTexture] = param1;
               this.addItem(_loc11_);
            }
         }
      }
   }
}
